<?php
class Document extends DB
{
    var $_table = 'document';

    function getList()
    {
	$this->_query = sprintf
	(
	    'SELECT `%1$s`.*,'
		.'(SELECT `id` FROM `customer` WHERE `customer`.`account_id`=`%1$s`.`account_id`) AS `customer_id`'
	    .' FROM `%1$s`'
	    .' %2$s %3$s',
	    $this->_table,
	    $this->_where($this->_filter,'AND',false),
	    $this->_order('`id`',false)
	);

	return $this->_getDataArray();
    }
}

class DocTTK extends Document
{
    function __construct()
    {
	parent::__construct();
	$this->_filter[] = array('`type`','ttk');
    }

    function setPeriod($start,$stop)
    {
	if ( !empty($start) && !empty($stop) )
	{
	    $this->_filter[] = sprintf('`document_date` BETWEEN \'%s\' AND \'%s\'', $start, $stop);
	}
	elseif ( empty($stop) )
	{
	    $this->_filter[] = sprintf('`document_date` BETWEEN \'%1$s\' AND CONCAT(LAST_DAY(DATE(\'%1$s\')),\' 23:59:59\')', $start);
	}

	return $this;
    }

    function setCustomer($id)
    {
	if ( is_numeric($id) )
	{
	    $this->_filter[] = sprintf('(SELECT `id` FROM `customer` WHERE `customer`.`account_id`=`%1$s`.`account_id`)=%2$d',$this->_table,$id);
	}
	elseif ( is_array($id) )
	{
	    $this->_filter[] = sprintf('(SELECT `id` FROM `customer` WHERE `customer`.`account_id`=`%1$s`.`account_id`) IN(%2$s)',$this->_table,implode(',',$id));
	}

	return $this;
    }

    function isPrinted($bool)
    {
	if ( $bool = true )
	{
	    $this->_filter[] = '`print` IS NOT NULL';
	}
	else
	{
	    $this->_filter[] = '`print` IS NULL';
	}

	return $this;
    }
}
?>
